js.underas.container.MenuTab = new Class({
	"Extends":js.underas.core.Component
	,"_config":null
	,"initialize":function(p_config){
		this.parent('div','');
		this._config = p_config;
	}
	,"addTab":function(label,boxM,iconeM,tmItens,tabid){
		var menuToPut = new Element('li',{'class':'navitem','id':'tabmenu_'+tabid,'html':'<a class="" href="#"><span class="imgI glyphicon iconeinput glyphicon-'+iconeM+'"></span><label class="hidden-xs">'+label+'</label></a>'});
		$$(this._config.target).getParent("div.menu_main").getElement(".tab_content_menu").grab(boxM);
		$$(this._config.target).grab(menuToPut);
		
	}
	,"setIcon":function(p_src){
	}
	,"appendTo":function(p_idFather){
		$$(p_idFather).grab(this.getEle());
	}
	,"setDataProvider":function(p_dta){	
		var dta = new js.underas.util.ArrayList(p_dta);	
		var tml = dta.size();
		for(var z = 0;z<tml;z++){
			var itemTmp = dta.get(z);
			this.criarTabNova(itemTmp.label,itemTmp.icone,dta.get(z)["children"],z);
		}
		this.appendTo(this._config["target"]);
	}
	,"criarTabNova":function(label,picone,childrens,tabid){
		var boxM = new Element('ul',{'id':'tabmenu_'+tabid+'_l','class':'nav nav-pills'});
		boxM.setStyle("display","none");
		var tml = childrens.length;
		var iconeM = "file";
		for(var z = 0;z<tml;z++){				
				var btTab = new js.underas.controller.LinkButton(childrens[z].label);				
				if(childrens[z].icone!=" "){
					btTab.setIcon(childrens[z].icone);
					btTab.getEle(".imgI").addClass("hidden-xs");
					iconeM = childrens[z].icone;
				}
				//if(window[childrens[z].funcao]){
				if(childrens[z].tela!=""){
					btTab.getEle().set({"data-varmod":childrens[z].tela,"data-actmod":childrens[z].funcao,"data-iconmod":iconeM,"data-titlemod":childrens[z].label});
				}else{
					btTab.setEnable(false);
				}
				boxM.grab(btTab.getEle());
		}	
		this.addTab(label,boxM,picone,tml,tabid);
	}
});




window.addEvent('domready', function() {	
	
	//esconder os menus ao clicar no icone home
	$$("#logo_menu").addEvent('click',function(event){
		event.preventDefault();	
		$$("#sidebar li.navitem_simple").removeClass("navitem_simple");
		$$("#sidebar").getParent("div.menu_main").getChildren(".tab_content_menu ul").each(function(item){
			item.setStyle("display","none");	
		});	
	});
	
	//muda de menu
	$$("#sidebar").addEvent('click:relay(li)',function(event){
		event.preventDefault();	
		$$("#sidebar li.navitem_simple").removeClass("navitem_simple");
		this.addClass("navitem_simple");
		$$("#sidebar").getParent("div.menu_main").getChildren(".tab_content_menu ul").each(function(item){
			item.setStyle("display","none");	
		});	
		$$("#"+this.get("id")+"_l").setStyle("display","block");
	});
	
	//mudanca das abas
	$$("#navbarlist").addEvent('click:relay(a)', function(event, target){
		event.preventDefault();	
		window["modview_"+this.get("data-modviewuid")].show(true);
	});
	
	//clicando no botao fechar aba
	$$("#navbarlist").addEvent('click:relay(a span.x_rmv)', function(event, target){
		event.preventDefault();	
		var uidToRemove = this.get("data-modviewuid");
		window["modview_"+uidToRemove].show(false).showNav(false).destroy();
		window["modview_"+uidToRemove] = null;
		$("uid_"+uidToRemove).destroy();
		$("aba_s_"+uidToRemove).destroy();
		var tmpFirstTab = $$("#navbarlist").getFirst("li a");
		if(tmpFirstTab.get){
			//var firstAfter = tmpFirstTab.get("data-modviewuid")[0];
			if(tmpFirstTab.get("data-modviewuid")){
				var firstAfter = tmpFirstTab.get("data-modviewuid")[0];
				window["modview_"+firstAfter].show(true);
			};
		};		
	});
	
	
	//clicando no menu e carregando o modulo
	$$(".tab_content_menu").addEvent('click:relay(li)',function(event){
			event.preventDefault();	
			var _linkM = this;			
			var nextload = false;
			var moduleToLoad = _linkM.get("data-varmod"); 	
			var varModuleToLoadTmpM = moduleToLoad.split(".");
			var varModuleToLoadTmp = varModuleToLoadTmpM[varModuleToLoadTmpM.length-1];
			var varModuleToLoadTmpCapt=varModuleToLoadTmp;
			varModuleToLoadTmp=varModuleToLoadTmp.toLowerCase();
			//ainda nao foi solicitado
			if(!_linkM.hasClass("loading_module")){			
				//marcando link como loading	
				_linkM.addClass("loading_module");
				nextload = true;
			}else if(window[varModuleToLoadTmp]){
				//o modulo esta invisivel
				//nextload = true;
				window[varModuleToLoadTmp].getModView().show(true).showNav(true);
			}else if(window[varModuleToLoadTmpCapt]&&!window[varModuleToLoadTmp]){
				//o modulo foi destruido chamando uma nova instancia
				nextload = true;
			};			
			if(nextload){
				js.underas.core.Underas.loadModule({"mod":_linkM.get("data-varmod"),"act":_linkM.get("data-actmod"),"icon":_linkM.get("data-iconmod"),"title":_linkM.get("data-titlemod")});
			};
			$$("#sidebar li.navitem_simple").removeClass("navitem_simple");
			$$("#sidebar").getParent("div.menu_main").getChildren(".tab_content_menu ul").each(function(item){
				item.setStyle("display","none");	
			});				
	});	
});
